//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   ItemCategoryDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:24:52  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc05Itemcatgry;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class ItemCategoryDao extends Dao {

    private static final String CLASS_NAME = ItemCategoryDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    public Teesc05Itemcatgry getItemCategoryByPK(Integer itemCategoryNumber,
                                                 Teesg01Plant plant)
            throws PersistenceException {

        String METHOD_NAME = "getItemCategoryByPK";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesc05Itemcatgry result = null;
        Teesc05Itemcatgry pk = new Teesc05Itemcatgry();
        pk.setEesc05ItemcatgryNumberK(itemCategoryNumber);
        pk.setEesc05PlantCodeC(plant);
        result = (Teesc05Itemcatgry) super.getObjById(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Busca Item Categories segun el codigo y nombre cargados en itemCategory
     * 
     * @param itemCategory
     * @return Los Item Categories encontrados
     * @throws PersistenceException
     */
    public Collection<Teesc05Itemcatgry> findItemCategories(
                                                            Teesc05Itemcatgry itemCategory)
            throws PersistenceException {

        String METHOD_NAME = "findItemCategories";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesc05Itemcatgry> res = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm
                .newQuery("findItemCategories", Teesc05Itemcatgry.class);
        query.setParameters(itemCategory);
        //query.setOrdering("eesc05ItemcatgryNameN ascending");
        res = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }

    /**
     * Da de alta el Item Category pasado por parametro
     * 
     * @param itemCategory
     * Debe tener cargado el nombre del Item Category
     * @throws PersistenceException
     */
    public void createItemCategory(Teesc05Itemcatgry itemCategory)
            throws PersistenceException {

        String METHOD_NAME = "createItemCategory";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(itemCategory);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Devuelve el itemCategory cuyo code es itemCategoryCode listo para
     * realizarle las actualizaciones pertinentes
     * 
     * @param plant
     * Planta a la que pertenece el usuario que realiza la actualizacion
     * @param itemCategoryCode
     * Code del Item Category a actualizar
     * @return
     * @throws PersistenceException
     */
    public Teesc05Itemcatgry getItemCategoryForUpdate(Teesg01Plant plant,
                                                      Integer itemCategoryCode)
            throws PersistenceException {

        String METHOD_NAME = "getItemCategoryForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesc05Itemcatgry result = null;
        Teesc05Itemcatgry pk = new Teesc05Itemcatgry();

        pk.setEesc05PlantCodeC(plant);
        pk.setEesc05ItemcatgryNumberK(itemCategoryCode);
        result = (Teesc05Itemcatgry) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Elimina de forma definitiva de la base de datos el itemCategory pasado
     * por parametro
     * 
     * @param itemCategory
     * Debe haber sido obtenido de la base de datos
     * @throws PersistenceException
     */
    public void deleteItemCategory(Teesc05Itemcatgry itemCategory)
            throws PersistenceException {

        String METHOD_NAME = "deleteItemCategory";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(itemCategory);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    public Collection<Teesc05Itemcatgry> findAllItemCategories(
                                                               Teesg01Plant plant)
            throws PersistenceException {

        String METHOD_NAME = "findAllItemCategories";
        log.entering(CLASS_NAME, METHOD_NAME);

        String[] fields = new String[1];
        Object[] values = new Object[1];
        Collection<Teesc05Itemcatgry> res = null;

        fields[0] = "eesc05PlantCodeC";
        values[0] = plant;

        res = super.findAll(new Teesc05Itemcatgry(), fields, values);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }
}
